﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using LightSwitchApplication.UserCode;
namespace LightSwitchApplication
{
    public partial class PaymentListsListDetail
    {
        private bool IsLoaded;
        private DialogHelper dialogHelper;
        private DialogHelper dialogGetPO;
        partial void PaymentListsListDetail_Created()
        {
            // Write your code here.  
            dialogHelper.InitializeUI();
            dialogGetPO.InitializeUI();
            this.FindControl("PaymentListDetails1").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(PaymentListsListDetail_ControlAvailable);
        }

        void PaymentListsListDetail_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            var datagrid = e.Control as System.Windows.Controls.DataGrid;
            if(datagrid!=null)
            {
                foreach(var column in  datagrid.Columns)
                {
                    if(column.SortMemberPath == "RefID")
                    {
                        column.IsReadOnly = true;
                    }
                }
            }                
        }

        partial void PaymentListsListDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.
            dialogHelper = new DialogHelper(this.PaymentLists, "Group3", 500, -1);
            dialogGetPO = new DialogHelper(this.PaymentListDetails, "Group4", 400, 200);
        }

        partial void btnOK_Execute()
        {
            // Write your code here.
            dialogHelper.DialogOK(); 
        }

        partial void BtnCancel_Execute()
        {
            // Write your code here.
            dialogHelper.DialogCancel();    
        }

        partial void PaymentListListAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.
            result = dialogHelper.CanAdd(); 
        }

        partial void PaymentListListAddAndEditNew_Execute()
        {
            // Write your code here.
            dialogHelper.AddEntity();   
        }

        partial void GetPO_Execute()
        {
            // Write your code here.
            dialogGetPO.EditSelectedEntity("Chọn hóa đơn tham chiếu");
        }

        partial void btnGetPO_OK_Execute()
        {
            // Write your code here.
            dialogGetPO.DialogOK();
        }

        partial void btnGetPO_Cancel_Execute()
        {
            // Write your code here.
            dialogGetPO.DialogCancel(); 
        }

        partial void PaymentLists_Loaded(bool succeeded)
        {
            if (!succeeded) return;
            if(string.IsNullOrEmpty(PO_DocumentID)) return;
            IsLoaded = true;
        }

        partial void PaymentListsListDetail_Activated()
        {
            // Write your code here.
            if (IsLoaded)
            {
                decimal amtPayment = 0;
                foreach (var payment in this.PaymentLists)
                {
                    foreach (var detail in payment.PaymentListDetails.Where(o => o.RefID == PO_DocumentID))
                    {
                        amtPayment = amtPayment + detail.Amount.Value;
                    }
                }

                var PO = this.DataWorkspace.LSWData.PurchaseOrders.Where(o => o.DocumentID == PO_DocumentID).SingleOrDefault();

                if (PO == null) return;

                if (amtPayment >= PO.AmountTotal) return;

                var itemnew = this.PaymentLists.AddNew();

                if (PO.CustsVend != null)
                    itemnew.ObjectID = PO.CustsVend.CustVendID;

                var detailnew = itemnew.PaymentListDetails.AddNew();
                detailnew.RefID = PO_DocumentID;
                detailnew.Amount = PO.AmountTotal - amtPayment;

                IsLoaded = false;
                return;
            }

            if (IsOpenAndNew != null && IsOpenAndNew.Value)
            {
                PaymentListListAddAndEditNew_Execute();
                IsOpenAndNew = false;
                return;
            }
        }
    }
}
